﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using OpenCvSharp;
using OpenCvSharp.Flann;
using PropertyChanged;

namespace VIA.SiMoJi.Domain.Inspection
{
    [Serializable]
    [AddINotifyPropertyChangedInterface]
    public class RunningStatus
    {
        public string Name { get; set; }
        /// <summary>
        /// 当前工位运行的步骤
        /// </summary>
        public int Step { get; set; }
        public RunningStatus() { }
        public RunningStatus(string name) { Name = name; }
        public void Save()
        {
            var str = JsonConvert.SerializeObject(this);
            string path = Path.Combine(Environment.CurrentDirectory, "RunningRecord", Name);
            path = Path.ChangeExtension(path, ".json");
            if (!File.Exists(path))
            {
                var stream = File.Create(path);
                stream.Close();
            }
            using StreamWriter sw = new StreamWriter(path);
            sw.WriteLine(str);
        }
        public static T GetRecord<T>(string name) where T : RunningStatus, new()
        {
            string path = Path.Combine(Environment.CurrentDirectory, "RunningRecord", name);
            path = Path.ChangeExtension(path, ".json");
            if (!File.Exists(path))
            {
                var file = File.Create(path);
                file.Dispose();
            }
            using StreamReader sw = new StreamReader(path);
            var str = sw.ReadToEnd();
            T record = JsonConvert.DeserializeObject<T>(str);
            record ??= new T() { Name = name };
            return record;
        }
    }
}
